//
//  SISSTenantReservationRecordViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/12/26.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSTenantReservationRecordViewController.h"
#import "SISSTenantTakeLookRecordViewController.h"
#import "SISSTenantTakeLookCell.h"
#import "SISSTenantTakeLookAppointmentModel.h"
#import "SISSTenantProgressModel.h"
#import "SISSNetworkingManager+TenantProgress.h"
#import "SISSNetworkingManager+Progress.h"

@interface SISSTenantReservationRecordViewController ()<UITableViewDelegate,UITableViewDataSource,SISSTenantTakeLookCellDelegate>

/** tableView */
@property (weak, nonatomic) SISSBaseTableView *tableView;
/** 带看记录控制器 */
@property (weak, nonatomic) SISSTenantTakeLookRecordViewController *tenantTakeLookRecordViewController;
/* 预约记录模型数组 */
@property (strong, nonatomic) NSArray<SISSTenantTakeLookAppointmentModel *> *reservationArr;

@end

@implementation SISSTenantReservationRecordViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf requestTenantReservationRecordData];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self requestTenantReservationRecordData];
}

#pragma mark -- UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWithMessage:@"没有相关预约记录..." ifNecessaryForRowCount:self.reservationArr.count];
    return self.reservationArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SISSTenantTakeLookCell *cell = [tableView dequeueReusableCellWithIdentifier:tenantTakeLookCellReuseID forIndexPath:indexPath];
    cell.delegate = self;
    cell.takeLookAppointmentModel = self.reservationArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark -- SISSTenantTakeLookCellDelegate
- (void)tenantTakeLookCell:(SISSTenantTakeLookCell *)cell clickButtonWithIndex:(NSInteger)index {
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    NSInteger state = index == 0 ? 2 : (index == 1 ? 1 : 0);
    
    [kNetworkManager updateLeaseTakeLookAppointmentStateWithId:self.reservationArr[indexPath.row].ID state:state success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) {
            NSString *tipStr = state == 1 ? @"预约成功" : @"已拒绝";
            [SISSTools showSVSuccessTitle:tipStr andTime:2 andColor:YES];
            [self requestTenantReservationRecordData];
        }
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - 点击事件
#pragma mark -- segmentControl点击事件
- (void)segmentControlbutClick:(UISegmentedControl *)sender {
    
    if (sender.selectedSegmentIndex == 0) {
        self.tenantTakeLookRecordViewController.view.hidden = YES;
        [self requestTenantReservationRecordData];
    } else {
        self.tenantTakeLookRecordViewController.view.hidden = NO;
        [self.tenantTakeLookRecordViewController requestTenantTakeLookRecordData];
    }
}


#pragma mark - navigationItem
- (void)setupNavigationItems {
    
    [super setupNavigationItems];
    
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"预约记录", @"带看记录"]];
    segmentControl.selectedSegmentIndex = 0;
    segmentControl.tintColor = COLOR(0x249DC5);
    [segmentControl addTarget:self action:@selector(segmentControlbutClick:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentControl;
    
}


#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    SISSBaseTableView *tableView = [[SISSBaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    // 自动行高
    tableView.estimatedRowHeight = 180;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView registerClass:[SISSTenantTakeLookCell class] forCellReuseIdentifier:tenantTakeLookCellReuseID];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    // 自动布局
    //    CGFloat margin = 10;
    //    CGFloat bottomViewHeight = 0;
    WeakSelf
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(weakSelf.view);
        //        make.bottom.mas_equalTo(-bottomViewHeight);
    }];
    
    
    // 添加带看记录合作视图
    SISSTenantTakeLookRecordViewController *tenantTakeLookRecordViewController = [[SISSTenantTakeLookRecordViewController alloc] init];
    tenantTakeLookRecordViewController.tenantProgressModel = self.tenantProgressModel;
    tenantTakeLookRecordViewController.isHistory = self.isHistory;
    [self.view addSubview:tenantTakeLookRecordViewController.view];
    [self addChildViewController:tenantTakeLookRecordViewController];
    //    TenantTakeLookRecordViewController.view.frame = CGRectOffset(self.view.frame, 0, 64);
    [tenantTakeLookRecordViewController didMoveToParentViewController:self];
    [tenantTakeLookRecordViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
    
    self.tenantTakeLookRecordViewController = tenantTakeLookRecordViewController;
    self.tenantTakeLookRecordViewController.view.hidden = YES;
}

#pragma mark -- 获取预约记录数据
- (void)requestTenantReservationRecordData {
    
    WeakSelf
    [kNetworkManager requestTenantTakelookAppointmentWithArbpId:self.tenantProgressModel.arbpId success:^(NSArray<SISSTenantTakeLookAppointmentModel *> *takeLookArr) {
            weakSelf.reservationArr = takeLookArr;
    
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
            }];
    
        } failed:^(NSError *error) {
    
            [weakSelf.tableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
